import Vue from "vue"
import App from "./App.vue"
import router from "./router"
import ElementUI from "element-ui";
import "element-ui/lib/theme-chalk/index.css";
import axios from "axios";
const instance = axios.create({
  baseURL: 'http://192.168.43.7:1013' 
});
let vueInstance;
// 请求拦截器
instance.interceptors.request.use(
    config => {
        const token = localStorage.getItem("token");
        if (token) {
            console.log("localStorage的token为：", token);
            config.headers.Authorization = `Bearer ${token}`;
        }
        return config;
    },
    error => {
        return Promise.reject(error);
    }
);
// 响应拦截器
instance.interceptors.response.use(
    response => {
        console.log("response", response);
        if (response.data) {
            const {code}=response.data;
            console.log("code:", code);
            if (code === 0) {
                vueInstance.$message.error("登录超时，请重新登录");
                vueInstance.$router.push("/login");
                return Promise.reject(new Error('登录超时'));
            }
            return response;
        } else {
            console.log("响应数据结构不符合预期");
            return response;
        }
    },
    error => {
        return Promise.reject(error);
    }
);
Vue.prototype.$axios = instance;
Vue.use(ElementUI);
Vue.config.productionTip = false;
vueInstance = new Vue({
    router,
    render: h => h(App)
}).$mount("#app");